package org.gary.base.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;
import org.gary.base.model.Image;
import org.gary.base.service.ImageService;
import org.gary.base.service.XxxImageService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author lengreen
 *
 */
@Component
@Scope("prototype")
public class ImageAction extends ActionSupport implements ServletResponseAware {
	private static final long serialVersionUID = 7978850860319931181L;
	private HttpServletResponse response;
	
	private XxxImageService xxxImageService;
	private ImageService imageService;
	private Image image;
	/**删除模式*/
	private String mode;
	/**图片主人*/
	private String who;
	/**结果字符*/
	private String result;

	/**
	 * 删除
	 * @throws Exception
	 */
	public void delete() throws Exception {
		if("delete".equals(mode)) {
			xxxImageService.deleteByImageId(getWho(), image.getId());
			imageService.delete(imageService.load(Image.class, image.getId()));
			result = "{'result':'delete'}";
		} else if("update".equals(mode)){
			imageService.update(Image.class, new Object[]{image.getId()}, false);
			result = "{'result':'update'}";
		} else {
			result = "{'result':'none'}";
		}
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(result);
	}
	
	@Resource
	public void setImageService(ImageService imageService) {
		this.imageService = imageService;
	}

	public ImageService getImageService() {
		return imageService;
	}

	public void setImage(Image image) {
		this.image = image;
	}

	public Image getImage() {
		return image;
	}

	public void setMode(String mode) {
		this.mode = mode;
	}

	public String getMode() {
		return mode;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setWho(String who) {
		this.who = who;
	}

	public String getWho() {
		return who;
	}

	@Resource
	public void setXxxImageService(XxxImageService xxxImageService) {
		this.xxxImageService = xxxImageService;
	}

	public XxxImageService getXxxImageService() {
		return xxxImageService;
	}
}
